package com.powernobug.service.impl;

import com.github.pagehelper.PageHelper;
import com.powernobug.bean.model.MarketKeyword;
import com.powernobug.bean.model.MarketKeywordExample;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.param.KeywordListParam;
import com.powernobug.mapper.MarketKeywordMapper;
import com.powernobug.service.KeywordService;
import com.powernobug.util.MyBatisUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 */
/*

 */
public class KeywordServiceImpl implements KeywordService {
    @Override
    public PageVo list(KeywordListParam keywordListParam) {
        String sort = keywordListParam.getSort();
        String order = keywordListParam.getOrder();
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {

            MarketKeywordMapper keywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);
            MarketKeywordExample example = new MarketKeywordExample();

            // 排序
            example.setOrderByClause(sort + " " + order);
            keywordMapper.selectByExample(example);

            // 分页
            Integer page = keywordListParam.getPage();
            Integer limit = keywordListParam.getLimit();
            PageHelper.startPage(page, limit);

            List<MarketKeyword> marketIssues = keywordMapper.selectByExample(example);

            return PageVo.list(marketIssues);
        }
    }

    @Override
    public Map create(Map keywordCreateVo) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            MarketKeywordMapper mapper = sqlSession.getMapper(MarketKeywordMapper.class);

            MarketKeyword marketKeyword = new MarketKeyword();
            // 封装实例
            BeanUtils.copyProperties(marketKeyword, keywordCreateVo);
            mapper.insertSelective(marketKeyword); // 入库
            
            // 反回data
            if(keywordCreateVo.containsKey("isHot")){
                if(Objects.equals(keywordCreateVo.get("isHot"),"false")){
                    keywordCreateVo.put("isHot","普通");
                }else {
                    keywordCreateVo.put("isHot","推荐");
                }
            }
            if(keywordCreateVo.containsKey("isDefault")){
                if(Objects.equals(keywordCreateVo.get("isDefault"),"false")){
                    keywordCreateVo.put("isDefault","非默认");
                }else {
                    keywordCreateVo.put("isDefault","默认");
                }
            }
            
            sqlSession.commit(); // 事务提交
            return keywordCreateVo;
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public MarketKeyword update(MarketKeyword marketKeyword) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            MarketKeywordMapper mapper = sqlSession.getMapper(MarketKeywordMapper.class);

            mapper.updateByPrimaryKey(marketKeyword);

            sqlSession.commit();
            return marketKeyword;
        }
    }

    @Override
    public Map updateMap(Map updateInfoMap) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            MarketKeywordMapper keywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);

            MarketKeyword marketKeyword = new MarketKeyword();
            BeanUtils.copyProperties(marketKeyword, updateInfoMap);

            
            keywordMapper.updateByPrimaryKeySelective(marketKeyword); // 向数据库更新数据
            sqlSession.commit(); // 事务提交
            
            return updateInfoMap;
            
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delete(MarketKeyword marketKeyword) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            MarketKeywordMapper mapper = sqlSession.getMapper(MarketKeywordMapper.class);
            mapper.deleteByPrimaryKey(marketKeyword.getId());
            sqlSession.commit();
        }
    }

    @Override
    public void deleteMap(Map deleteInfoMap) {
         Integer id = (Integer) deleteInfoMap.get("id");

        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            MarketKeywordMapper keywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);
            keywordMapper.deleteByPrimaryKey(id);
            sqlSession.commit();
        }
    }
}
